package com.ciaojian.timer.task;

import cn.hutool.core.date.DateUnit;
import com.ciaojian.core.mapper.CouponCodeMapper;
import com.ciaojian.core.mapper.CouponMapper;
import com.ciaojian.core.model.Coupon;
import com.ciaojian.core.model.vo.CouponExpiredTimeVo;
import com.ciaojian.core.util.LogUtil;
import com.ciaojian.core.util.RedisUtil;
import com.ciaojian.miniapp.api.WxMiniAppApi;
import com.ciaojian.miniapp.model.request.submessage.BaseTemplate;
import com.ciaojian.miniapp.model.request.submessage.TakeCouponMessage;
import com.ciaojian.miniapp.model.request.submessage.TemplateIdEnum;
import com.ciaojian.miniapp.model.response.SubMessageSendResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;

@Component
public class CoupensScheduled {

    @Resource
    private CouponCodeMapper couponCodeMapper;
    @Resource
    private CouponMapper couponMapper;

    /**
     * 服务提醒  优惠券即将过期
     */
    @Scheduled(cron = "0 30 14 * * ?")   //  14:30:00 每天
    public void couponExpiredTime() {
        int size = couponCodeMapper.couponExpiredNum() + 20;
        int page = 20;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 设置list大小 ,list一次行不能太多数据
        for (int current = 0; current <= size; current+=page) {

            List<CouponExpiredTimeVo> couponExpiredTimeVoList = couponCodeMapper.couponExpiredTime(current,page);

            int userId =0;
            for (CouponExpiredTimeVo couponExpiredTimeVo : couponExpiredTimeVoList) {
                try {
                    if (couponExpiredTimeVo.getUserId() == userId) {
                        // 同时间多张券要过期就跳过,只发送一次
                        continue;
                    }
                    userId = couponExpiredTimeVo.getUserId();

                    String expiredTime = simpleDateFormat.format(couponExpiredTimeVo.getExpiredTime());
                    BigDecimal reduction = couponExpiredTimeVo.getReduction();
                    int money = reduction.intValue();

                    //
                    BaseTemplate baseTemplate = BaseTemplate.builder(TemplateIdEnum.COUPON_TO_EXPIRE)
                            .touser(couponExpiredTimeVo.getOpenid())
                            .data(TakeCouponMessage.builder()
                                    .thing4(couponExpiredTimeVo.getTitle())
                                    .number1(money)
                                    .time3(expiredTime)
                                    .thing5("可到潮剪小程序预约剪发")
                                    .thing6("潮剪各门店通用")
                                    .build()
                            );
                    System.out.println(1);
                    try {
                        SubMessageSendResponse response = WxMiniAppApi.subscribeMessageSend(baseTemplate);
                        if (response.getErrcode() == 0) {

                        } else {
                            LogUtil.writeBusinessLog("优惠券到期通知失败: {}", response.getErrmsg());
                        }
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                        LogUtil.writeErrorLog(e.getMessage());
                    }

                }catch (Exception e){

                }

            }
        }

    }
}
